//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System.ComponentModel;
using System.Reflection;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Users;
namespace DotNetNuke.UI.WebControls
{
	public class StandardEditorInfoAdapter : IEditorInfoAdapter
	{
		private object DataSource;
		private string FieldName;
		public StandardEditorInfoAdapter(object dataSource, string fieldName)
		{
			this.DataSource = dataSource;
			this.FieldName = fieldName;
		}
		private EditorInfo GetEditorInfo(object dataSource, PropertyInfo objProperty)
		{
			EditorInfo editInfo = new EditorInfo();
			editInfo.Name = objProperty.Name;
			editInfo.Value = objProperty.GetValue(dataSource, null);
			editInfo.Type = objProperty.PropertyType.AssemblyQualifiedName;
			editInfo.Attributes = objProperty.GetCustomAttributes(true);
			editInfo.Category = string.Empty;
			object[] categoryAttributes = objProperty.GetCustomAttributes(typeof(CategoryAttribute), true);
			if (categoryAttributes.Length > 0)
			{
				CategoryAttribute category = (CategoryAttribute)categoryAttributes[0];
				editInfo.Category = category.Category;
			}
			if (!objProperty.CanWrite) {
				editInfo.EditMode = PropertyEditorMode.View;
			} else {
				object[] readOnlyAttributes = objProperty.GetCustomAttributes(typeof(IsReadOnlyAttribute), true);
				if (readOnlyAttributes.Length > 0)
				{
					IsReadOnlyAttribute readOnlyMode = (IsReadOnlyAttribute)readOnlyAttributes[0];
					if (readOnlyMode.IsReadOnly) {
						editInfo.EditMode = PropertyEditorMode.View;
					}
				}
			}
			editInfo.Editor = "UseSystemType";
			object[] editorAttributes = objProperty.GetCustomAttributes(typeof(EditorAttribute), true);
			if (editorAttributes.Length > 0)
			{
				EditorAttribute editor = null;
				for (int i = 0; i <= editorAttributes.Length - 1; i++) {
					if (((EditorAttribute)editorAttributes[i]).EditorBaseTypeName.IndexOf("DotNetNuke.UI.WebControls.EditControl") >= 0) {
						editor = (EditorAttribute)editorAttributes[i];
						break;
					}
				}
				if (editor != null) {
					editInfo.Editor = editor.EditorTypeName;
				}
			}
			editInfo.Required = false;
			object[] requiredAttributes = objProperty.GetCustomAttributes(typeof(RequiredAttribute), true);
			if (requiredAttributes.Length > 0)
			{
				RequiredAttribute required = (RequiredAttribute)requiredAttributes[0];
				if (required.Required) {
					editInfo.Required = true;
				}
			}
			editInfo.ControlStyle = new Style();
			object[] StyleAttributes = objProperty.GetCustomAttributes(typeof(ControlStyleAttribute), true);
			if (StyleAttributes.Length > 0)
			{
				ControlStyleAttribute attribute = (ControlStyleAttribute)StyleAttributes[0];
				editInfo.ControlStyle.CssClass = attribute.CssClass;
				editInfo.ControlStyle.Height = attribute.Height;
				editInfo.ControlStyle.Width = attribute.Width;
			}
			editInfo.LabelMode = LabelMode.Left;
			object[] labelModeAttributes = objProperty.GetCustomAttributes(typeof(LabelModeAttribute), true);
			if (labelModeAttributes.Length > 0)
			{
				LabelModeAttribute mode = (LabelModeAttribute)labelModeAttributes[0];
				editInfo.LabelMode = mode.Mode;
			}
			editInfo.ResourceKey = string.Format("{0}_{1}", dataSource.GetType().Name, objProperty.Name);
			editInfo.ValidationExpression = string.Empty;
			object[] regExAttributes = objProperty.GetCustomAttributes(typeof(RegularExpressionValidatorAttribute), true);
			if (regExAttributes.Length > 0)
			{
				RegularExpressionValidatorAttribute regExAttribute = (RegularExpressionValidatorAttribute)regExAttributes[0];
				editInfo.ValidationExpression = regExAttribute.Expression;
			}
			editInfo.Visibility = UserVisibilityMode.AllUsers;
			return editInfo;
		}
		private PropertyInfo GetProperty(object dataSource, string fieldName)
		{
			if (dataSource != null) {
				BindingFlags Bindings = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
				PropertyInfo objProperty = dataSource.GetType().GetProperty(fieldName, Bindings);
				return objProperty;
			} else {
				return null;
			}
		}
		public EditorInfo CreateEditControl()
		{
			EditorInfo editInfo = null;
			PropertyInfo objProperty = GetProperty(DataSource, FieldName);
			if (objProperty != null) {
				editInfo = GetEditorInfo(DataSource, objProperty);
			}
			return editInfo;
		}
		public bool UpdateValue(PropertyEditorEventArgs e)
		{
			bool changed = e.Changed;
			object oldValue = e.OldValue;
			object newValue = e.Value;
			bool _IsDirty = Null.NullBoolean;
			if (DataSource != null) {
				PropertyInfo objProperty = DataSource.GetType().GetProperty(e.Name);
				if (objProperty != null) {
					if ((!(object.ReferenceEquals(newValue, oldValue))) || changed)
					{
						objProperty.SetValue(DataSource, newValue, null);
						_IsDirty = true;
					}
				}
			}
			return _IsDirty;
		}
		public bool UpdateVisibility(PropertyEditorEventArgs e)
		{
			bool _IsDirty = Null.NullBoolean;
			return _IsDirty;
		}
	}
}
